package com.zzw.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzw.springboot.annotation.Log;
import com.zzw.springboot.bean.SysLog;
import com.zzw.springboot.service.SysLogService;
import com.zzw.springboot.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 系统日志控制器
 */
@Api(tags = "系统日志管理")
@RestController
@RequestMapping("/sysLog")
public class SysLogController {

    @Autowired
    private SysLogService sysLogService;

    /**
     * 分页查询日志
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @param sysLog 查询条件
     * @return 查询结果
     */
    @ApiOperation("分页查询日志")
    @GetMapping("/list")
    @Log(module = "系统日志", type = "查询", description = "分页查询日志列表")
    public Result list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       SysLog sysLog) {
        // 创建查询条件包装器
        LambdaQueryWrapper<SysLog> queryWrapper = Wrappers.lambdaQuery();

        // 根据操作用户名模糊查询
        if (StringUtils.hasText(sysLog.getUsername())) {
            queryWrapper.like(SysLog::getUsername, sysLog.getUsername());
        }

        // 根据操作模块查询
        if (StringUtils.hasText(sysLog.getModule())) {
            queryWrapper.like(SysLog::getModule, sysLog.getModule());
        }

        // 根据操作类型查询
        if (StringUtils.hasText(sysLog.getType())) {
            queryWrapper.eq(SysLog::getType, sysLog.getType());
        }

        // 根据操作状态查询
        if (sysLog.getStatus() != null) {
            queryWrapper.eq(SysLog::getStatus, sysLog.getStatus());
        }

        // 按时间倒序排序
        queryWrapper.orderByDesc(SysLog::getCreateTime);

        // 执行分页查询
        Page<SysLog> page = sysLogService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }

    /**
     * 获取日志详情
     * @param id 日志ID
     * @return 日志详情
     */
    @ApiOperation("获取日志详情")
    @GetMapping("/{id}")
    @Log(module = "系统日志", type = "查询", description = "获取日志详情")
    public Result getInfo(@PathVariable("id") Long id) {
        SysLog log = sysLogService.getById(id);
        if (log != null) {
            return Result.success(log);
        }
        return Result.error("日志不存在");
    }

    /**
     * 删除日志
     * @param id 日志ID
     * @return 删除结果
     */
    @ApiOperation("删除日志")
    @DeleteMapping("/{id}")
    @Log(module = "系统日志", type = "删除", description = "删除单条日志")
    public Result delete(@PathVariable("id") Long id) {
        boolean result = sysLogService.removeById(id);
        if (result) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    /**
     * 批量删除日志
     * @param ids 日志ID列表
     * @return 删除结果
     */
    @ApiOperation("批量删除日志")
    @DeleteMapping("/batch/{ids}")
    @Log(module = "系统日志", type = "删除", description = "批量删除日志")
    public Result batchDelete(@PathVariable("ids") String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        boolean result = sysLogService.removeByIds(idList);
        if (result) {
            return Result.success("批量删除成功");
        }
        return Result.error("批量删除失败");
    }

    /**
     * 清空日志
     * @return 清空结果
     */
    @ApiOperation("清空日志")
    @DeleteMapping("/clean")
    @Log(module = "系统日志", type = "删除", description = "清空全部日志")
    public Result clean() {
        boolean result = sysLogService.remove(Wrappers.emptyWrapper());
        if (result) {
            return Result.success("清空成功");
        }
        return Result.error("清空失败");
    }
}
